/*------------------------------------------------------------------- EXAMPLE of an embedded SQL C Program for DB2. Connect to database CS3DB3 for this. This APP fetches and prints each of the tuples from ONE.SAILOR (in database CS3DB3). --------------------------------------------------------------------*/ #include #include #include #include #include #include #define EXIT 0 #define NOEXIT 1 /*-------------------------------------------------------------------- Include DB2's SQL error reporting facility. --------------------------------------------------------------------*/ EXEC SQL INCLUDE SQLCA ; /*-------------------------------------------------------------------- Declare the SQL interface variables. --------------------------------------------------------------------*/ EXEC SQL BEGIN DECLARE SECTION ; short sid; short sage; short rating; char sname[7]; EXEC SQL END DECLARE SECTION ; /*-------------------------------------------------------------------- Declare variables to be used in the following C program. --------------------------------------------------------------------*/ char msg[1025]; int rc; int errcount; /*-------------------------------------------------------------------- This macro prints the message in the SQLCA if the return code is 0 and the SQLCODE is not 0. --------------------------------------------------------------------*/ #define PRINT_MESSAGE() \ { \ if (rc == 0 && sqlca.sqlcode != 0) \ { \ sqlaintp(msg, 1024, 0, &sqlca); \ printf("%s\n",msg); \ } \ } /*-------------------------------------------------------------------- This macro prints out all feilds in the SQLCA. --------------------------------------------------------------------*/ #define DUMP_SQLCA() \ { \ printf("********** DUMP OF SQLCA **********************\n"); \ printf("SQLCAID: %s\n", sqlca.sqlcaid); \ printf("SQLCABC: %d\n", sqlca.sqlcabc); \ printf("SQLCODE: %d\n", sqlca.sqlcode); \ printf("SQLERRML: %d\n", sqlca.sqlerrml); \ printf("SQLERRMC: %s\n", sqlca.sqlerrmc); \ printf("SQLERRP: %s\n", sqlca.sqlerrp); \ printf("SQLERRD[0]: %d\n", sqlca.sqlerrd[0]); \ printf("SQLERRD[1]: %d\n", sqlca.sqlerrd[1]); \ printf("SQLERRD[2]: %d\n", sqlca.sqlerrd[2]); \ printf("SQLERRD[3]: %d\n", sqlca.sqlerrd[3]); \ printf("SQLERRD[4]: %d\n", sqlca.sqlerrd[4]); \ printf("SQLERRD[5]: %d\n", sqlca.sqlerrd[5]); \ printf("SQLWARN: %s\n", sqlca.sqlwarn); \ printf("SQLSTATE: %s\n", sqlca.sqlstate); \ printf("********** END OF SQLCA DUMP *******************\n"); \ } /*-------------------------------------------------------------------- This macro prints the message in the SQLCA if one exists. If the return code is not 0 or the SQLCODE is not expected, an error occurred and must be recorded. --------------------------------------------------------------------*/ #define CHECK_SQL(code,text_string,eExit) \ { \ PRINT_MESSAGE(); \ if (rc != 0 || sqlca.sqlcode != code) { \ printf("%s\n",text_string); \ printf("Expected code = %d\n",code); \ if (rc == 0) { \ DUMP_SQLCA(); \ } \ else printf("RC: %d\n",rc); \ errcount += 1; \ if (eExit == EXIT) goto errorexit; \ } \ } /*-------------------------------------------------------------------- The PROGRAM. --------------------------------------------------------------------*/ main (int argc, char *argv[]) { EXEC SQL CONNECT TO CS3DB3; CHECK_SQL(0, "Connect failed", EXIT); /* Find the name and age of sailor SID. */ EXEC SQL DECLARE sailorC CURSOR FOR SELECT SID, SNAME, AGE, RATING FROM sailor; CHECK_SQL(0, "The SELECT query failed.", EXIT); EXEC SQL OPEN sailorC; printf("sid sname age rating\n"); while (1) { EXEC SQL FETCH sailorC INTO :sid, :sname, :sage, :rating; if (SQLCODE != 0) break; printf("%3d %-15s %3d %3d\n", sid, sname, sage, rating); } EXEC SQL CLOSE sailorC; errorexit: EXEC SQL CONNECT RESET; }